home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_05 / 9n05044a < prev    next >
Text File  |  1990-10-28  |  5KB  |  203 lines

  1.  
  2. /*
  3.  *      xlib1.c
  4.  *      An X Window (Xlib)-based program
  5.  *      written for the C Users Journal.
  6.  *
  7.  *      Link with the X library, e.g.,
  8.  *
  9.  *              cc -o xlib1 xlib1.c -lX11
  10.  *
  11.  *      Define SYSV if you have malloc()
  12.  *      declared in stdlib.h.
  13.  *
  14.  */
  15.  
  16. #include <stdio.h>
  17.  
  18. #ifdef SYSV
  19. #include <stdlib.h>
  20. #endif
  21.  
  22. #include <X11/Xlib.h>
  23. #include <X11/Xutil.h>
  24.  
  25. /*
  26.  *      We have a hard-coded size and location
  27.  */
  28. #define X_LOCATION      10
  29. #define Y_LOCATION      20
  30. #define WIDTH           200
  31. #define HEIGHT          200
  32.  
  33.  
  34. main( argc, argv )
  35.  
  36. int     argc;
  37. char    *argv[];
  38.  
  39. {       /* main */
  40.         Display         *display;
  41.         int             screen;
  42.         Window          rootwindow;
  43.         Window          window;
  44.         XSizeHints      *sizehints;
  45.         GC              gc;
  46.         XEvent          event;
  47.         int             done;
  48.  
  49.         /*
  50.          * Connect to an X server.
  51.          */
  52.         display = XOpenDisplay( (char *) NULL );
  53.  
  54.         if ( display == (Display *) NULL )
  55.                 {
  56.                 fprintf( stderr, "Error opeing in display\n" );
  57.                 exit( 1 );
  58.                 }
  59.  
  60.         screen     = DefaultScreen( display );
  61.         rootwindow = RootWindow( display, screen );
  62.  
  63.         /*
  64.          * Create a window
  65.          */
  66.         window = XCreateSimpleWindow( display,
  67.                         rootwindow,     /* parent */
  68.                         X_LOCATION, Y_LOCATION,
  69.                         WIDTH, HEIGHT,
  70.                         1,              /* border width */
  71.                         BlackPixel( display, screen ),
  72.                         WhitePixel( display, screen ) );
  73.  
  74.         /*
  75.          * Set up hints about the window
  76.          */
  77.         sizehints = (XSizeHints *) malloc( sizeof(XSizeHints) );
  78.  
  79.         sizehints->x      = X_LOCATION;
  80.         sizehints->y      = Y_LOCATION;
  81.         sizehints->width  = WIDTH;
  82.         sizehints->height = HEIGHT;
  83.         sizehints->flags  = PPosition | PSize;
  84.  
  85.         /* 
  86.          * Use XSetWMProperties() in R4
  87.          *      
  88.          */
  89.         XSetStandardProperties( display, window,
  90.                 "Xlib1",        /* window name */
  91.                 "Xlib1",        /* icon name */
  92.                 (Pixmap) None,  /* Icon pixmap */
  93.                 argv, argc,
  94.                 sizehints );
  95.  
  96.     free( sizehints );
  97.  
  98.         /*
  99.          * Ask for Expose and mouse (Button) input
  100.          */
  101.         XSelectInput( display, window,
  102.                 ButtonPressMask | ExposureMask );
  103.  
  104.  
  105.         /*
  106.          * Create a graphics 
  107.          * context to draw with.
  108.          */
  109.         gc = XCreateGC( display, window,
  110.                 0L, (XGCValues *) NULL );
  111.  
  112.         XSetForeground( display, gc, 
  113.                 BlackPixel( display, screen ) );
  114.  
  115.  
  116.         /*
  117.          * Make Window appear on the screen
  118.          */
  119.         XMapWindow( display, window );
  120.         XFlush( display );
  121.         
  122.         done = False;
  123.  
  124.         while( !done )
  125.                 {
  126.                 XNextEvent( display, &event );
  127.  
  128.                 if ( event.type == ButtonPress )
  129.                         {
  130.                         XCloseDisplay( display );
  131.  
  132.                         exit( 0 );
  133.                         }
  134.  
  135.                 if ( event.type == Expose ) 
  136.                         {
  137.                         /*
  138.                          * Only redraw when all
  139.                          * Expose events are in
  140.                          */
  141.                         if ( event.xexpose.count == 0 )
  142.                                 {
  143.                                 Redraw( display, window, gc, 
  144.                                         0, 0,
  145.                                         WIDTH, HEIGHT );
  146.                                 }
  147.                         }
  148.                 }
  149.  
  150.  
  151. }       /* main */
  152.  
  153. Redraw( display, window, gc, x, y, width, height )
  154.  
  155. Display *display;
  156. Window  window;
  157. GC      gc;
  158. int     x, y;
  159. int     width, height;
  160.  
  161. {       /* Redraw */
  162.         int     i, x1, y1;
  163.  
  164.         if ( ( width == 0 ) || ( height == 0 ) )
  165.                 {
  166.                 return;
  167.                 }
  168.  
  169.         x1 = x;
  170.         y1 = y;
  171.  
  172.         for( i = 0; i < 10; i++ )
  173.                 {
  174.                 x1 += width  / 10;
  175.  
  176.                 XDrawLine( display, window, gc,
  177.                         x1, y + 5,
  178.                         x1, y + 10 );
  179.  
  180.                 y1 += height / 10;
  181.  
  182.                 XDrawLine( display, window, gc,
  183.                         x + 5,  y1,
  184.                         x + 10, y1 );
  185.                 }
  186.  
  187.         for( i = 0; i < 10; i++ )
  188.                 {
  189.                 x += width  / 10;
  190.                 y += height / 10;
  191.  
  192.                 XFillRectangle( display, window, gc,
  193.                         x, y,
  194.                         6, 6 );
  195.                 }
  196.  
  197.         XFlush( display );
  198.  
  199. }       /* Redraw */
  200.  
  201. /* end of file */
  202.  
  203.